package com.agentlab.imitation.sms;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.logging.Level;
import java.util.logging.Logger;



/**
* 发送短信基础类
* @author administration
*
*/
public class SmsBase {
//private static String x_id="actionfdl";
//private static String x_pwd="action023";

private static String x_id="actionfdl";
private static String x_pwd="cqaction";
public  static int x_number = 99;

public  static String SendSmsAll(String sbmobile,String content) {
	String result ="";
	String[] mobiles = sbmobile.split(",");
	// 根据逗号 判断 100以内的位置
	int y = Integer.valueOf((mobiles.length/x_number)+1);
	String[] armob = new String[y];
	try {
		int j=1 ;
		StringBuffer sbstr = new StringBuffer();
		for(int i=0;i< mobiles.length;){
			if( i < j*x_number){
				sbstr.append(mobiles[i]).append(",");
			}else{
				sbstr.append(mobiles[i]);
				armob[j-1] = sbstr.toString();
				sbstr = new StringBuffer();
				j++;
			}
			i++;
		}
		if(sbstr.toString().length()>0)
		armob[j-1] = sbstr.deleteCharAt(sbstr.length()-1).toString();
		//发送
		
		for(int i=0;i< armob.length;i++){
			result += "__"+i+"__"+ SendSms(armob[i],content);
		}
		Logger log = Logger.getLogger(SmsBase.class.getName());
		log.log(Level.INFO,result);
	} catch (Exception e) {
		e.printStackTrace();
	}
	return result;
}

/**
* 
*@author Administrator
*@param mobile 接收短信手机号码
*@param content 短信内容
**@param mobile 接收短信手机号码
*/
	public static String SendSms(String mobile,String content) throws UnsupportedEncodingException{
			//Integer x_ac=10;//发送信息
			HttpURLConnection httpconn = null;
			String result="-20";
			//String memo = content.length()<70?content.trim():content.trim().substring(0, 70);
			StringBuilder sb = new StringBuilder();
			sb.append("http://service.winic.org/sys_port/gateway/?");
			sb.append("id=").append(x_id);
			sb.append("&pwd=").append(x_pwd);
			sb.append("&to=").append(mobile);
			sb.append("&content=").append(URLEncoder.encode(content, "gbk"));  //注意乱码的话换成gb2312编码
			try {
				URL url = new URL(sb.toString());
				httpconn = (HttpURLConnection) url.openConnection();
				BufferedReader rd = new BufferedReader(new InputStreamReader(httpconn.getInputStream()));
				result = rd.readLine();
				rd.close();
			} catch (MalformedURLException e) {
				
				e.printStackTrace();
			} catch (IOException e) {
				//输入输出异常
				e.printStackTrace();
			} finally{
				if(httpconn!=null){
					httpconn.disconnect();
					httpconn=null;
				}
			}
			return result;
	}
}
